package top.oauth2.sms.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.oauth2.common.response.Message;
import top.oauth2.common.response.ResponseMessage;
import top.oauth2.sms.entity.SendSmsVo;
import top.oauth2.sms.enums.SmsTypeEnum;
import top.oauth2.sms.exception.BaseException;
import top.oauth2.sms.service.SelectSmsService;

import javax.validation.Valid;

/**
 * @author xphu
 * @version 1.0
 * @date 2021/9/16 14:24
 */
@Slf4j
@RestController
@RequestMapping("sms")
public class SmsController {

    @Autowired
    SelectSmsService selectSmsService;

    @PostMapping("/send")
    public Message sendSmsMsg(@Valid @RequestBody SendSmsVo sendSmsVo){
        boolean flage = false;
        try {
            flage = selectSmsService.sendSmsMsg(sendSmsVo.getPhone(), sendSmsVo.getCode(), sendSmsVo.getTemplateId(), SmsTypeEnum.getSmsType(sendSmsVo.getType()));
        } catch (BaseException e) {
            log.error(e.getMessage());
        }
        if (flage) {
            return ResponseMessage.builder().success("发送成功").message();
        } else {
            return ResponseMessage.builder().error("发送失败，出现未知异常").message();
        }
    }

}


